//获取应用实例
var app = getApp();
var index = 1;
Page({
  data: {
    list: [],
    hasMore: true,
    // 是否显示loading
    showLoading: false,
    // loading提示语
    loadingMessage: '',
    // 是否显示toast
    showToast: false,
    // 提示消息
    toastMessage: '',
    classifyGuid:'',
    keyword:'',
    canSee: false,
    touch_end: 0,
    touch_start: 0,
  },
  editAddress: function (event) {
    let that = this;
    //触摸时间距离页面打开的毫秒数
    var touchTime = that.data.touch_end - that.data.touch_start;
    console.log(touchTime);
    console.log(event)
    var index = event.currentTarget.id;
    var item = that.data.list[index];
    var linkmanGuid = item.linkmanGuid;
    console.log(linkmanGuid)
    if (touchTime > 350) {
      if (!this.data.canSee) {
        return;
      }
      wx.showModal({
        title: '提示',
        content: '确定要删除吗？',
        success: function (res) {
          if (res.confirm) {
            deleteLinkman(that,linkmanGuid);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '../linkDetail/linkDetail?linkmanGuid=' + linkmanGuid
      })
    }
  },
  //按下事件开始
  mytouchstart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束
  mytouchend: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },
  // 显示loading提示
  showLoading(loadingMessage) {
    this.setData({ showLoading: true, loadingMessage });
  },

  // 隐藏loading提示
  hideLoading() {
    this.setData({ showLoading: false, loadingMessage: '' });
  },

  // 显示toast消息
  showToast(toastMessage) {
    this.setData({ showToast: true, toastMessage });
  },

  // 隐藏toast消息
  hideToast() {
    this.setData({ showToast: false, toastMessage: '' });
  },
  onLoad: function (options) {
    this.setData({
      classifyGuid: options.classifyGuid
    });
    console.log(this.data.classifyGuid);
    // var self = this;
    // this.showLoading('加载中...');
    // index = 1;
    // getData(self);
  },
  onShow:function(){
    var self = this;
    // this.showLoading('加载中...');
    index = 1;
    getData(self);
    var userguid = wx.getStorageSync('OPEN_ID');
    if (userguid) {
      getUserByUserGuid(self, userguid);
    }
  },
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    getData(that);
  },
  onReachBottom: function () {
    var that = this
    if (!that.data.showLoading) {
      getData(that);
    }
  },
  phoneCall: function (event){
    let that = this;
    console.log(event);
    // 取得下标
    var index = parseInt(event.currentTarget.dataset.id);
    // 取出id值
    var linkmanTelephone = that.data.list[index].linkmanTelephone;
    console.log(linkmanTelephone);
    wx.makePhoneCall({
      phoneNumber: linkmanTelephone //仅为示例，并非真实的电话号码
    })
  },
  // 小程序 点击搜索 按钮时  事件
  confirmTap: function (event) {
    console.log(event.detail.value);
    this.setData({ keyword: event.detail.value});
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    getData(that);
  },
  toAddlinkman: function (event) {
    wx.navigateTo({
      url: "../addLinkman/addLinkman"
    })
  },
})
//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/getAllLinkmanByclassifyGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        keyword: self.data.keyword,
        classifyGuid: self.data.classifyGuid,
        index: index,
        currentPageCount: 20
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      self.hideLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.list.length == 0) {
          self.setData({
            hasMore: false,
          })
        } else {
          if (res.data.UserArea.list.length < 20) {
            self.setData({
              hasMore: false,
            })
          }
          if(index == 1){
            self.setData({
              list: res.data.UserArea.list
            });
          }else{
            self.setData({
              list: self.data.list.concat(res.data.UserArea.list)
            });
          }
        }
        index++;
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })

}

var getUserByUserGuid = function (self, userguid) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/getUserByUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userGuid: userguid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.isAdmin == 1) {
          self.setData({
            canSee: true
          })
        }
      }
    }
  })
}

var deleteLinkman = function (self,linkmanGuid) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/deleteLinkman', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuids: linkmanGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        });
        var that = self
        that.setData({
          list: [],
          hasMore: true,
          showLoading: true,
        })
        index = 1;
        that.showLoading('加载中...');
        getData(that);
      } else {
        wx.showToast({
          title: '删除失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}